package com.jackrain.nea.fc.dl.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.dl.api.ViewDLPurInToF4QueryCmd;
import com.jackrain.nea.fc.dl.mapper.ViewDLPurInToF4Mapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.FastDateFormat;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 采购入库单
 *
 * @author wuwenxue
 * @date 2018/05/22
 **/
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "dl")
public class ViewDLPurInToF4QueryCmdImpl extends CommandAdapter implements ViewDLPurInToF4QueryCmd {

    private final static FastDateFormat DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd");

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        ValueHolder vh = new ValueHolder();
        ViewDLPurInToF4Mapper viewDLPurInToF4Mapper = ApplicationContextHandle.getBean(
                ViewDLPurInToF4Mapper.class);
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(
                map.get("param"), "yyyy-MM-dd HH:mm:ss"), Feature.OrderedField);
        log.info("ViewDLPurInToF4QueryCmdImpl--param-->{}",param);

        if (null == param || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }

        Object[] ids = param.getJSONArray("IDS").toArray();
        String action = param.getString("action");
        JSONArray jsonArray = new JSONArray();
        if ("accept".equals(action)) {
            //验收
            List<HashMap> list = viewDLPurInToF4Mapper.selPurInData(ids);
            for (HashMap hashMap : list) {
                Long id = TypeUtils.castToLong(hashMap.get("ID"));
                String billNo = TypeUtils.castToString(hashMap.get("BILL_NO"));

                Date billDate = TypeUtils.castToDate(hashMap.get("BILL_DATE"));
                String formatBillDate = DATE_FORMAT.format(billDate);

                hashMap.put("BILL_DATE", formatBillDate);

                //采购订单单号
                Long billId = TypeUtils.castToLong(hashMap.get("ORDER_BILL_ID"));
                if (null != billId) {
                    String orderNo = viewDLPurInToF4Mapper.selBillNo(billId);
                    hashMap.put("ORDER_NO", orderNo);
                }

                //采购类型
                Integer billType = TypeUtils.castToInt(hashMap.get("BILL_TYPE"));
                if (null != billType) {
                    Integer f4BillType;
                    switch (billType) {
                        case 1:
                            f4BillType = 3;
                            break;
                        case 2:
                            f4BillType = 4;
                            break;
                        case 3:
                            f4BillType = 5;
                            break;
                        case 4:
                            f4BillType = 6;
                            break;
                        default:
                            f4BillType = null;
                            break;
                    }
                    hashMap.put("BILL_TYPE", f4BillType);
                }

                List<HashMap> itemList = viewDLPurInToF4Mapper.selPurInItemData(id);
                JSONArray itemArray = new JSONArray();
                for (HashMap itemH : itemList) {
                    itemH.put("BILL_NO", billNo);
                    //管理成本价
                    BigDecimal priceMgt = TypeUtils.castToBigDecimal(itemH.get("PRICE_MGT"));
                    //管理成本金额
                    BigDecimal amtMgt = TypeUtils.castToBigDecimal(itemH.get("AMT_MGT"));
                    //不含税采购价
                    BigDecimal priceNonTax = TypeUtils.castToBigDecimal(itemH.get("PRICE_NON_TAX"));
                    //不含税采购金额
                    BigDecimal amtNonTax = TypeUtils.castToBigDecimal(itemH.get("AMT_NON_TAX"));

                    if (null != priceMgt) {
                        itemH.put("PRICE_MGT", priceMgt);
                    } else {
                        itemH.put("PRICE_MGT", 0);
                    }

                    if (null != amtMgt) {
                        itemH.put("AMT_MGT", amtMgt);
                    } else {
                        itemH.put("AMT_MGT", 0);
                    }

                    if (null != priceNonTax) {
                        itemH.put("PRICE_NON_TAX", priceNonTax);
                    } else {
                        itemH.put("PRICE_NON_TAX", 0);
                    }

                    if (null != amtNonTax) {
                        itemH.put("AMT_NON_TAX", amtNonTax);
                    } else {
                        itemH.put("AMT_NON_TAX", 0);
                    }
                    JSONObject itemjs = new JSONObject(itemH);
                    itemArray.add(itemjs);
                }

                JSONObject js = new JSONObject(hashMap);
                js.put("DL_B_PUR_IN_ITEM", itemArray);
                JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(js,
                        "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
                jsonArray.add(param1);

            }
        }

        vh.put("data", jsonArray);
        return vh;
    }


}
